import axios from 'axios'
import { message } from 'ant-design-vue'

// axios.defaults.baseURL = process.env.VUE_APP_BASEURL;
// axios.defaults.withCredentials = true; // 允许携带cookie
interface ApiStatus {
  /**
   * 返回的数据
   */
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  result: any
  /**
   * 接口状态
   */
  status: string
}
/**
 * axios的post请求
 * @param api 请求的api
 * @param params 请求的参数
 * @param config 请求的头配置信息
 * @returns 请求处理后的数据
 */
const post = (api: string, params?: any, config?: any): Promise<ApiStatus> => {
  let headers = {
    user_token_xm: localStorage.token
  }
  if (config?.headers) {
    headers = {
      ...headers,
      ...config.headers
    }
  }

  return axios
    .post(
      api,
      params,
      { headers }
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
    )
    .then((res: any) => {
      if (res.status === 200) {
        if (res.data.code === 0) {
          return {
            result: res.data.data,
            status: 'success'
          }
        }
        message.error(res.data.msg)
        return {
          result: null,
          status: 'fail'
        }
      }
      message.error('网络错误')
      return {
        result: null,
        status: 'fail'
      }
    })
    .catch((error) => {
      message.error(error)
      return {
        result: null,
        status: 'fail'
      }
    })
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const sendFormData = (api: string, params?: any): Promise<ApiStatus> => {
  const headers = {
    'Content-Type': ' multipart/form-data',
    user_token_xm: localStorage.token
  }
  return axios
    .post(api, params, { headers })
    .then((res: any) => {
      if (res.status === 200) {
        if (res.data.code === 0) {
          return {
            result: res.data.data,
            status: 'success'
          }
        }
        message.error(res.data.msg)
        return {
          result: null,
          status: 'fail'
        }
      }
      message.error('网络错误')
      return {
        result: null,
        status: 'fail'
      }
    })
    .catch((error) => {
      message.error(error)
      return {
        result: null,
        status: 'fail'
      }
    })
}
export { post, sendFormData }
